//
// Created by 86138 on 2025/5/17.
//
#include "LinkStack.h"

namespace sweetcookie3_1 {
    void InitLinkStack(LinkStack &S) {
        S = nullptr;
    }

    void DestroyLinkStack(LinkStack &S) {
        StackNode *p;
        while (S) {
            p = S;
            free(p);
            S = S->Next;
        }
        S = nullptr;
    }

    Status Push(LinkStack &S, SElemType e) {
        StackNode *p = (StackNode *) malloc(sizeof(StackNode));
        if (!p) exit(OVERFLOW);
        p->Data = e;
        p->Next = S;
        S = p;
        return OK;
    }

    Status Pop(LinkStack &S, SElemType &e) {
        if (S == nullptr) {
            printf("栈空，不能出栈\n");
            return ERROR;
        }
        StackNode *p = S;
        e = p->Data;
        S = S->Next;
        free(p);
        return OK;
    }

    bool StackEmpty(LinkStack S) {
        return S == nullptr;
    }
}

//using namespace sweetcookie3_1;
//int main() {
//    LinkStack S;
//    InitLinkStack(S);
//    for (int i = 10; i <= 100; i = i + 10) {
//        Push(S, i);
//    }
//    StackNode *p = S;
//    while (p) {
//        printf("%d ", p->Data);
//        p = p->Next;
//    }
//    printf("\n");
//    SElemType e;
//    Pop(S, e);
//    p = S;
//    while (p) {
//        printf("%d ", p->Data);
//        p = p->Next;
//    }
//    printf("\n");
//}